<?php
namespace app\common\logic\home;
use app\common\model\TemplateGroupModel;
use app\common\model\MytaskModel;
use app\common\model\TemplateAttrModel;
use app\common\model\ClassprogressModel;
use think\Db;

class Template
{
	public function index1($group_id,$userId){
		$data = TemplateGroupModel::get($group_id);
		
	    //表头
	    if(!empty($data['header'])){
	        $header = json_decode($data['header'],true);
	        $content = json_decode($header['content'],true);
	        foreach($content as $val){
	            $data_header[$val] = '';
	        }
	        $header['content'] = json_encode($data_header);
	        $param['header'] = json_encode($header);
	    }
	    //表尾
	    if(!empty($data['footer'])){
	        $footer = json_decode($data['footer'],true);
	        $content = json_decode($footer['content'],true);
	        foreach($content as $val){
	            $data_footer[$val] = '';
	        }
	        $footer['content'] = json_encode($data_footer);
	        $param['footer'] = json_encode($footer);
	    }
	    //分类
	    $param['template_category_id'] = $data['category_id'];

	    $mid = $data['template_id'];
	    $mm=Db::name('template')->where("id=".$mid)->field('name,description')->find();
	    $progress=Db::name('classprogress')->where(['mid'=>$mid])->select();
	    $param['content']=$progress;
	    $param['name']=$mm['name'];
	    $param['description']=$mm['description'];
	    $param['uid']=$userId;
	    $param['create_time']=date('Y-m-d H:i:s');
	    $param['update_time']=date('Y-m-d H:i:s');
	    $param['t_id'] = $mid;
	    $param['type'] = 1;
	    $param['group_id'] = $group_id;
        $param['p_category_id'] = $data['p_category_id'];

      
	    $taskid= db('mytask')->insertGetId($param);
	    return $taskid;
	}

	public function index($group_id,$userId){
		$data = TemplateGroupModel::get($group_id);
		
	    //表头
	    if(!empty($data['header'])){
	        $header = json_decode($data['header'],true);
	        $content = json_decode($header['content'],true);
	        foreach($content as $val){
	            $data_header[$val] = '';
	        }
	        $header['content'] = json_encode($data_header);
	        $param['header'] = json_encode($header);
	    }
	    //表尾
	    if(!empty($data['footer'])){
	        $footer = json_decode($data['footer'],true);
	        $content = json_decode($footer['content'],true);
	        foreach($content as $val){
	            $data_footer[$val] = '';
	        }
	        $footer['content'] = json_encode($data_footer);
	        $param['footer'] = json_encode($footer);
	    }
	    //分类
	    $param['template_category_id'] = $data['category_id'];

	    $mid = $data['template_id'];
	    $mm=Db::name('template')->where("id=".$mid)->field('name,description')->find();
	    $progress=Db::name('classprogress')->where(['mid'=>$mid])->select();
	    $param['content']=$progress;
	    $param['name']=$mm['name'];
	    $param['description']=$mm['description'];
	    $param['uid']=$userId;
	    $param['create_time']=date('Y-m-d H:i:s');
	    $param['update_time']=date('Y-m-d H:i:s');
	    $param['t_id'] = $mid;
	    $param['type'] = 2;
	    $param['group_id'] = $group_id;
        $param['p_category_id'] = $data['p_category_id'];
	    $taskid= db('mytask')->insertGetId($param);
	
    	if($taskid){
			$this->insertAnser1($userId,$taskid);
			$this->insertAnser2($userId,$taskid);
			$this->saveTask($userId,$taskid);
		} 
	   
	    return $taskid;
	}

	/**
	 * [添加默认图片]
	 * @param  [type] $userId [description]
	 * @param  [type] $taskid  [课题ID]
	 * @return [type]         [description]
	 */
	public function insertAnser1($userId,$taskid){
		$data['taskid'] = $taskid;
		$progressid = 3;
		$t_id = MytaskModel::where('id',$data['taskid'])->value('t_id');
		$classprogressid = ClassprogressModel::where('mid',$t_id)->where('progressid',$progressid)->value('id');
		$data['content'] = '1|http://'.$_SERVER['SERVER_NAME'].'/upload/dt1.jpg,1|http://'.$_SERVER['SERVER_NAME'].'/upload/dt2.jpg,&lt;&gt;';
		$data['pid'] = $progressid;
		$data['classprogressid'] = $classprogressid;
		$data['activation_num'] = 0;
		$data['status'] = 0;
        $data['userId']=$userId;
        $data['createTime']=date('Y-m-d h:i:s',time());
        $re=db('anser')->insert($data);
	}
	/**
	 * [添加默认感受]
	 * @param  [type] $userId [description]
	 * @param  [type] $taskid  [课题ID]
	 * @return [type]         [description]
	 */
	public function insertAnser2($userId,$taskid){
		$data['taskid'] = $taskid;
		$progressid = 4;
		$t_id = MytaskModel::where('id',$data['taskid'])->value('t_id');
		$classprogressid = ClassprogressModel::where('mid',$t_id)->where('progressid',$progressid)->value('id');
		$data['content'] = '0|通过参加此次大课堂活动，我了解到陶瓷文化是我国优秀的非物质文化遗产，体验到动手制作陶艺的乐趣。我热爱这项中华民族传统文化。,';
		$data['pid'] = $progressid;
		$data['classprogressid'] = $classprogressid;
		$data['activation_num'] = 0;
		$data['status'] = 0;
        $data['userId']=$userId;
        $data['createTime']=date('Y-m-d h:i:s',time());
        $re=db('anser')->insert($data);
	}

	public function saveTask($userId,$taskid){
		//已完成
	    $up_data['status'] = 3;
	    $up_data['com_time'] = date("Y-m-d H:i:s");
		$rs = MytaskModel::where('id',$taskid)->where('uid',$userId)->update($up_data);
	}
    
}
